{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Encode and Decode Strings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #design #array #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #设计 #数组 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: encode"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #字符串的编码与解码"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>请你设计一个算法，可以将一个&nbsp;<strong>字符串列表&nbsp;</strong>编码成为一个&nbsp;<strong>字符串</strong>。这个编码后的字符串是可以通过网络进行高效传送的，并且可以在接收端被解码回原来的字符串列表。</p>\n",
    "\n",
    "<p>1 号机（发送方）有如下函数：</p>\n",
    "\n",
    "<pre>string encode(vector&lt;string&gt; strs) {\n",
    "  // ... your code\n",
    "  return encoded_string;\n",
    "}</pre>\n",
    "\n",
    "<p>2 号机（接收方）有如下函数：</p>\n",
    "\n",
    "<pre>vector&lt;string&gt; decode(string s) {\n",
    "  //... your code\n",
    "  return strs;\n",
    "}\n",
    "</pre>\n",
    "\n",
    "<p>1 号机（发送方）执行：</p>\n",
    "\n",
    "<pre>string encoded_string = encode(strs);\n",
    "</pre>\n",
    "\n",
    "<p>2 号机（接收方）执行：</p>\n",
    "\n",
    "<pre>vector&lt;string&gt; strs2 = decode(encoded_string);\n",
    "</pre>\n",
    "\n",
    "<p>此时，2 号机（接收方）的 <code>strs2</code>&nbsp;需要和 1 号机（发送方）的 <code>strs</code> 相同。</p>\n",
    "\n",
    "<p>请你来实现这个&nbsp;<code>encode</code> 和&nbsp;<code>decode</code> 方法。</p>\n",
    "\n",
    "<p><strong>注意：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li>因为字符串可能会包含 256 个合法&nbsp;ascii 字符中的任何字符，所以您的算法必须要能够处理任何可能会出现的字符。</li>\n",
    "\t<li>请勿使用 &ldquo;类成员&rdquo;、&ldquo;全局变量&rdquo; 或 &ldquo;静态变量&rdquo; 来存储这些状态，您的编码和解码算法应该是非状态依赖的。</li>\n",
    "\t<li>请不要依赖任何方法库，例如 <code>eval</code>&nbsp;又或者是&nbsp;<code>serialize</code>&nbsp;之类的方法。本题的宗旨是需要您自己实现 &ldquo;编码&rdquo; 和 &ldquo;解码&rdquo; 算法。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [encode-and-decode-strings](https://leetcode.cn/problems/encode-and-decode-strings/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [encode-and-decode-strings](https://leetcode.cn/problems/encode-and-decode-strings/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"Hello\",\"World\"]', '[\"\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        encoded_string = ''\n",
    "\n",
    "        for s in strs:\n",
    "            encoded_string += s.replace(\"/\", \"//\") + \"/:\"\n",
    "        return encoded_string\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        decoded_strings = []\n",
    "\n",
    "        current_string = \"\"\n",
    "\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            if s[i:i+2] == '//':\n",
    "                current_string += '/'\n",
    "                i += 2\n",
    "            elif s[i:i+2] == \"/:\":\n",
    "                decoded_strings.append(current_string)\n",
    "                current_string = \"\"\n",
    "                i += 2\n",
    "            else:\n",
    "                current_string += s[i]\n",
    "                i += 1\n",
    "        return decoded_strings\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        return strs\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        return s\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        return '#$%^&'.join(strs)\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        return s.split('#$%^&')\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:             \n",
    "        return chr(257).join(strs)       \n",
    "    def decode(self, s: str) -> List[str]:        \n",
    "        return s.split(chr(257))\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        return '[]&&'.join(strs)\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        return s.split('[]&&')\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    '''\n",
    "    Prefix every string with its length - denoted by a 2-digit hex number.\n",
    "    Time complexity: O(n). \n",
    "    Space complexity: O(1) (neglecting the output string). Explanation: join() pre-computes the length of the string using O(1) space, allocates the required space, and constructs the string.\n",
    "    '''\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        return ''.join(f'{len(s):02x}{s}' for s in strs)\n",
    "\n",
    "    '''\n",
    "    Decode.\n",
    "    Time complexity: O(n). Space complexity: O(1).\n",
    "    '''\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        strs = []\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            length = int(s[i:i+2], 16)\n",
    "            strs.append(s[i+2:i+2+length])\n",
    "            i += 2 + length\n",
    "        return strs\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: [str]) -> str:\n",
    "        \"\"\"将字符串列表编码为单个字符串。\n",
    "\n",
    "        参数:\n",
    "            strs ([str]): 要编码的字符串列表。\n",
    "\n",
    "        返回:\n",
    "            str: 编码后的字符串。\n",
    "        \"\"\"\n",
    "        list_ = []  # 初始化空列表用于存储编码后的字符串\n",
    "        for i in strs:\n",
    "            # 对每个字符串，添加一个四字符长的长度信息（右对齐，不足部分用空格填充），然后将长度信息和字符串拼接\n",
    "            list_.append('{:4}'.format(len(i)) + i)\n",
    "        return ''.join(list_)  # 将所有编码后的字符串拼接为一个字符串并返回\n",
    "\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> [str]:\n",
    "        \"\"\"将单个编码字符串解码为字符串列表。\n",
    "\n",
    "        参数:\n",
    "            s (str): 要解码的字符串。\n",
    "\n",
    "        返回:\n",
    "            [str]: 解码后的字符串列表。\n",
    "        \"\"\"\n",
    "        index = 0  # 初始化索引用于遍历编码字符串\n",
    "        list_ = []  # 初始化空列表用于存储解码后的字符串\n",
    "        while index <= len(s) - 2:\n",
    "            # 从编码字符串中读取四字符长的长度信息，并转换为整数\n",
    "            char_long = int(s[index:index + 4])\n",
    "            # 根据长度信息提取相应的字符串，并添加到列表\n",
    "            list_.append(s[index + 4:index + char_long + 4])\n",
    "            # 更新索引，移动到下一个编码字符串的长度信息处\n",
    "            index += 4 + char_long\n",
    "        return list_  # 返回解码后的字符串列表\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        if len(strs) == 0:\n",
    "            return chr(258)\n",
    "        \n",
    "        return chr(258).join(strs)\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        return s.split(chr(258))\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    # Use 4 bytes as header to indicate the length of string.\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        res = []\n",
    "        for s in strs:\n",
    "            length = str(len(s))\n",
    "            header = \"0\" * (4 - len(length)) + length\n",
    "            res.append(header)\n",
    "            res.append(s)\n",
    "        return \"\".join(res)\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        n = len(s)\n",
    "        read = 0\n",
    "        res = []\n",
    "        while read < n:\n",
    "            header = s[read:read+4]\n",
    "            read += 4\n",
    "            res.append(s[read:read+int(header)])\n",
    "            read += int(header)\n",
    "        return res\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:       \n",
    "        return \"(!@#)\".join(strs)       \n",
    "    def decode(self, s: str) -> List[str]:        \n",
    "        return s.split(\"(!@#)\")\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        return '%00%'.join(strs)\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        return s.split('%00%')\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        return \"#-#\".join(strs)\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        return s.split(\"#-#\")\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "\n",
    "\n",
    "    def encode(self, strs: List[str]):\n",
    "        #if not strs:\n",
    "        #    return chr(257)\n",
    "        return chr(259).join(strs)\n",
    "        \n",
    "    def decode(self, s):\n",
    "        #if s == chr(257):\n",
    "        #    return []\n",
    "        return s.split(chr(259))\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: [str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        list_ = []\n",
    "        for i in strs:\n",
    "            list_.append('{:4}'.format(len(i)) + i)\n",
    "        return ''.join(list_)\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> [str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        index = 0\n",
    "        list_ = []\n",
    "        while index <= len(s) - 2:\n",
    "            char_long = int(s[index:index + 4])\n",
    "            list_.append(s[index + 4:index + char_long + 4])\n",
    "            index += 4 + char_long\n",
    "        return list_\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))\n",
    "\"\"\"\n",
    "作者：青碧凝霜\n",
    "链接：https://leetcode.cn/problems/encode-and-decode-strings/solutions/1010639/python3-ji-lu-fa-by-qing-bi-ning-shuang-z6mi/\n",
    "来源：力扣（LeetCode）\n",
    "著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs):\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "\n",
    "        ans = ''\n",
    "        ans += str(len(strs))\n",
    "        ans += ':'\n",
    "\n",
    "        for ss in strs:\n",
    "            ans += str(len(ss))\n",
    "            ans += '/'\n",
    "\n",
    "        ans += ''.join(strs)\n",
    "\n",
    "        return ans\n",
    "        \n",
    "\n",
    "    def decode(self, s):\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "\n",
    "        \n",
    "\n",
    "        len_of_list = 0\n",
    "        lens = []\n",
    "\n",
    "        i = 0\n",
    "        tmp = ''\n",
    "        while s[i] != ':':\n",
    "            tmp += s[i]\n",
    "            i += 1\n",
    "\n",
    "        len_of_list = int(tmp)\n",
    "        i += 1\n",
    "\n",
    "        for _ in range(len_of_list):\n",
    "            tmp = ''\n",
    "            while s[i] != '/':\n",
    "                tmp += s[i]\n",
    "                i += 1\n",
    "            lens.append(int(tmp))\n",
    "            i += 1\n",
    "\n",
    "        ans = []\n",
    "\n",
    "        for ll in lens:\n",
    "            ans.append(s[i:i+ll])\n",
    "            i += ll\n",
    "\n",
    "        return ans\n",
    "\n",
    "\n",
    "####################\n",
    "# class Codec:\n",
    "#     def encode(self, strs: List[str]) -> str:\n",
    "#         \"\"\"Encodes a list of strings to a single string.\n",
    "#         \"\"\"\n",
    "#         ans1 = str(len(strs)) + '#'\n",
    "#         ans2 = ''\n",
    "#         for s in strs:\n",
    "#             ans1 += str(len(ans2)) + '#'\n",
    "#             ans2 += s\n",
    "\n",
    "#         # print(ans1 + ans2)\n",
    "#         return ans1 + ans2\n",
    "        \n",
    "\n",
    "#     def decode(self, s: str) -> List[str]:\n",
    "#         \"\"\"Decodes a single string to a list of strings.\n",
    "#         \"\"\"\n",
    "#         s_list = s.split('#')\n",
    "#         len_s_list = len(s_list)\n",
    "\n",
    "#         n = int(s_list[0])\n",
    "#         id_list = []\n",
    "#         for i in range(n):\n",
    "#             id_list.append(int(s_list[i + 1]))\n",
    "        \n",
    "#         if n + 2 == len_s_list:\n",
    "#             ss = s_list[-1]\n",
    "#         else:\n",
    "#             ss = '#'.join(s[n + 1:])\n",
    "#         # print(ss)\n",
    "\n",
    "#         ans = []\n",
    "#         for i in range(len(id_list) - 1):\n",
    "#             ans.append(ss[id_list[i]: id_list[i + 1]])\n",
    "        \n",
    "#         print(id_list, i)\n",
    "#         # print(id_list[i+1])\n",
    "#         ans.append(ss[id_list[i+1]:]) \n",
    "        \n",
    "#         return ans\n",
    "        \n",
    "\n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "# # Your Codec object will be instantiated and called as such:\n",
    "# # codec = Codec()\n",
    "# # codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# ### 官1：非ascii字符来识别字符为空的情况(unichr函数，python3废弃--无法调试成功)\n",
    "# class Codec:\n",
    "#     def encode(self, strs):\n",
    "#         \"\"\"Encodes a list of strings to a single string.\n",
    "#         :type strs: List[str]\n",
    "#         :rtype: str\n",
    "#         \"\"\"\n",
    "#         if len(strs) == 0: \n",
    "#             # return unichr(258) ### python-2 有unichr函数\n",
    "#             return chr(258)\n",
    "        \n",
    "#         # encode here is a workaround to fix BE CodecDriver error\n",
    "#         # return unichr(257).join(x.encode('utf-8') for x in strs)\n",
    "#         return chr(257).join(x.encode('utf-8') for x in strs)\n",
    "        \n",
    "\n",
    "#     def decode(self, s):\n",
    "#         \"\"\"Decodes a single string to a list of strings.\n",
    "#         :type s: str\n",
    "#         :rtype: List[str]\n",
    "#         \"\"\"\n",
    "#         # if s == unichr(258): \n",
    "#         #     return []\n",
    "#         # return s.split(unichr(257))\n",
    "\n",
    "#         if s==chr(258):return []\n",
    "#         return s.split(chr(257))\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))\n",
    "\n",
    "# 官1：网友py3版\n",
    "class Codec:\n",
    "    def encode(self,strs):\n",
    "        if len(strs)==0:\n",
    "            return chr(258)\n",
    "\n",
    "        return chr(257).join(strs)\n",
    "\n",
    "    def decode(self,s):\n",
    "        if s==chr(258): return []\n",
    "        return s.split(chr(257))\n",
    "\n",
    "# 官2：基于http1.1使用的编码，不依赖与输入字符集\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        return chr(256).join(x for x in strs)\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        return s.split(chr(256))\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "\n",
    "    def encode(self, strs):\n",
    "        \"\"\"\n",
    "        Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        # Join all strings in the list with a separator.\n",
    "        # Each string is prefixed with its length and a separator.\n",
    "        return ''.join(f'{len(s):04d}{s}' for s in strs)\n",
    "\n",
    "    def decode(self, s):\n",
    "        \"\"\"\n",
    "        Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        # Initialize an empty list to store the decoded strings\n",
    "        strs = []\n",
    "        # Initialize a pointer to the start of the string\n",
    "        i = 0\n",
    "        # Loop until the end of the string is reached\n",
    "        while i < len(s):\n",
    "            # Read the length of the next string from the next 4 characters\n",
    "            length = int(s[i:i + 4])\n",
    "            # Move the pointer past the length prefix\n",
    "            i += 4\n",
    "            # Extract the next string using the length prefix\n",
    "            strs.append(s[i:i + length])\n",
    "            # Move the pointer to the start of the next string\n",
    "            i += length\n",
    "        return strs\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        res=[]\n",
    "        for word in strs:\n",
    "            res.append(word)\n",
    "            res.append(\"好\")\n",
    "        return \"\".join(res)\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        res=[]\n",
    "        pre=\"\"\n",
    "        for ch in s:\n",
    "            if ch!=\"好\":\n",
    "                pre=pre+ch\n",
    "            else:\n",
    "                res.append(pre)\n",
    "                pre=\"\"\n",
    "        return res\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "\n",
    "\n",
    "    def encode(self, strs: List[str]):\n",
    "        if not strs:\n",
    "            return chr(258)\n",
    "        return chr(257).join(strs)\n",
    "        \n",
    "    def decode(self, s):\n",
    "        if s == chr(258):\n",
    "            return []\n",
    "        return s.split(chr(257))\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        if len(strs) == 0:\n",
    "            return chr(258)\n",
    "        return chr(257).join(strs)\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        if len(s) == chr(258):\n",
    "            return []\n",
    "        return s.split(chr(257))\n",
    "            \n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        return '%00%'.join(strs)\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        return s.split('%00%')\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: [str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\"\"\"\n",
    "        if not strs:\n",
    "            return chr(258)\n",
    "        return chr(257).join(strs)\n",
    "\n",
    "    def decode(self, s: str) -> [str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\"\"\"\n",
    "        if s == chr(258):\n",
    "            return []\n",
    "        return s.split(chr(257))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        return '%00%'.join(strs)\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        return s.split('%00%')\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "\n",
    "    def encode(self, strs):\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \n",
    "        :type strs: List[str]\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        return ''.join(map(lambda s: f\"{len(s)}#{s}\", strs))\n",
    "        #这个匿名函数（或lambda函数）接收一个字符串s作为输入，然后返回一个新的字符串，该字符串首先包含s的长度（len(s)），接着是一个#字符，最后是s本身。\n",
    "        #map函数接收一个函数和一个可迭代对象（在这里是字符串列表strs），并返回一个迭代器，该迭代器为strs中的每个元素应用该函数。在这个例子中，对strs中的每个字符串s，map都应用前面描述的lambda函数，生成一个新的字符串，形式为\"<长度>#<原始字符串>\"。\n",
    "        #''.join(iterable)方法接收一个可迭代对象（通常是一个字符串列表或迭代器）并将其元素连接成一个单一的字符串。\n",
    "\n",
    "    def decode(self, s):\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \n",
    "        :type s: str\n",
    "        :rtype: List[str]\n",
    "        \"\"\"\n",
    "        res = []\n",
    "        i = 0\n",
    "\n",
    "        while i < len(s):\n",
    "            j = i\n",
    "            while s[j] !='#':\n",
    "                j += 1 #寻找到#号\n",
    "            length = int(s[i:j]) #切片到长度元素\n",
    "            i = j + 1 #j后面第一位是str开始的第一位\n",
    "            j = i +length #str第一位加上长度得到str的最后一位索引\n",
    "            res.append(s[i:j]) #或许str到res中\n",
    "            i = j #继续寻找一下一个str\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        res=[]\n",
    "        for word in strs:\n",
    "            res.append(word)\n",
    "            res.append(\"好\")\n",
    "        return \"\".join(res)\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        res=[]\n",
    "        pre=\"\"\n",
    "        for ch in s:\n",
    "            if ch!=\"好\":\n",
    "                pre=pre+ch\n",
    "            else:\n",
    "                res.append(pre)\n",
    "                pre=\"\"\n",
    "        return res\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        if len(strs) == 0:\n",
    "            return chr(258)\n",
    "        return chr(257).join(strs)\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        if s == chr(258):\n",
    "            return []\n",
    "        return s.split(chr(257))\n",
    "        \n",
    "#编码时：\n",
    "#使用chr(257)来作为分隔符\n",
    "\n",
    "#解码时：\n",
    "#使用chr(257)，然后s.split()来转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        if len(strs) == 0:\n",
    "            return chr(258)\n",
    "        \n",
    "        return chr(257).join(strs)\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        if s == chr(258):\n",
    "            return []\n",
    "        \n",
    "        return s.split(chr(257))\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        return \"*:*;\".join(strs)\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        return s.split(\"*:*;\")\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        res = []\n",
    "        for s in strs:\n",
    "            res.append(\"{:4d}\".format(len(s)))\n",
    "            res.append(s)\n",
    "        return \"\".join(res)\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        res = []\n",
    "        idx, n = 0, len(s)\n",
    "        while idx < n:\n",
    "            length = int(s[idx: idx + 4])\n",
    "            res.append(s[idx + 4: idx + 4 + length])\n",
    "            idx += 4 + length\n",
    "        return res\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "\n",
    "\n",
    "    def encode(self, strs: List[str]):\n",
    "        if not strs:\n",
    "            return chr(257)\n",
    "        return chr(259).join(strs)\n",
    "        \n",
    "    def decode(self, s):\n",
    "        if s == chr(257):\n",
    "            return []\n",
    "        return s.split(chr(259))\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        return '%00%'.join(strs)\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        return s.split('%00%')\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        return json.dumps(strs)\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        return json.loads(s)\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        encoded_str = ''\n",
    "        for s in strs:\n",
    "            encoded_str += str(len(s)) + '#' + s\n",
    "        return encoded_str\n",
    "        \n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        decoded_strs = []\n",
    "        idx = 0\n",
    "        while idx < len(s):\n",
    "            hash_idx = s.find('#', idx)\n",
    "            str_len = int(s[idx:hash_idx])\n",
    "            decoded_strs.append(s[hash_idx+1:hash_idx+1+str_len])\n",
    "            idx = hash_idx + 1 + str_len\n",
    "        return decoded_strs\n",
    "\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs):\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        :type strs: List[str]\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        if len(strs) == 0: \n",
    "            return chr(258)\n",
    "        \n",
    "        # encode here is a workaround to fix BE CodecDriver error\n",
    "        return chr(257).join(strs)\n",
    "        \n",
    "\n",
    "    def decode(self, s):\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        :type s: str\n",
    "        :rtype: List[str]\n",
    "        \"\"\"\n",
    "        if s == chr(258): \n",
    "            return []\n",
    "        return s.split(chr(257))\n",
    "\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        res = ''\n",
    "        for s in strs:\n",
    "            res += str(len(s)) + '#' + s \n",
    "        return res \n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        res = []\n",
    "        i = 0 \n",
    "        while i < len(s):\n",
    "            j = i \n",
    "            while s[j] != '#':\n",
    "                j += 1\n",
    "            length = int(s[i:j])\n",
    "            res.append(s[j + 1:j + 1 + length])\n",
    "            i = j + 1 + length \n",
    "        return res \n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        ans = \"\"\n",
    "        for s in strs:\n",
    "            ans += str(len(s)) + '#' + s\n",
    "        return ans\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        ans = []\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            j = i\n",
    "            while s[j] != \"#\":\n",
    "                j += 1\n",
    "            \n",
    "            length = int(s[i:j])\n",
    "            ans.append(s[j+1:j+1+length])\n",
    "\n",
    "            i = j+length+1\n",
    "        return ans\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        return '|'.join([s.replace('|', '\\\\|') for s in strs])\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        ret = []\n",
    "        tmp = \"\"\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            c = s[i]\n",
    "            if c == '|':\n",
    "                ret.append(tmp)\n",
    "                tmp = \"\"\n",
    "                i += 1\n",
    "            elif c == '\\\\' and i + 1 < len(s) and s[i + 1] == '|':\n",
    "                tmp += '|'\n",
    "                i += 2\n",
    "            else:\n",
    "                tmp += c\n",
    "                i += 1\n",
    "        ret.append(tmp)\n",
    "        return ret\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        temp = ''\n",
    "        for s in strs:\n",
    "            n = str(len(s))\n",
    "            temp += (4-len(n))*'0' + n\n",
    "            temp += s\n",
    "        return temp\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        i = 0\n",
    "        strs = []\n",
    "        while i < len(s):\n",
    "            length = int(s[i:i+4])\n",
    "            i += 4\n",
    "            strs.append(s[i:i+length])\n",
    "            i += length\n",
    "        return strs\n",
    "\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "\n",
    "\n",
    "    def encode(self, strs: List[str]):\n",
    "        if not strs:\n",
    "            return chr(258)\n",
    "        return chr(259).join(strs)\n",
    "        \n",
    "    def decode(self, s):\n",
    "        if s == chr(258):\n",
    "            return []\n",
    "        return s.split(chr(259))\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        return '|'.join([s.replace('|', '\\\\|') for s in strs])\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        ret = []\n",
    "        tmp = \"\"\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            c = s[i]\n",
    "            if c == '|':\n",
    "                ret.append(tmp)\n",
    "                tmp = \"\"\n",
    "                i += 1\n",
    "            elif c == '\\\\' and i + 1 < len(s) and s[i + 1] == '|':\n",
    "                tmp += '|'\n",
    "                i += 2\n",
    "            else:\n",
    "                tmp += c\n",
    "                i += 1\n",
    "        ret.append(tmp)\n",
    "        return ret\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        return ''.join(map(lambda s: f'{len(s)}#{s}', strs))\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        res = []\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            j = i\n",
    "            while s[j] != '#':\n",
    "                j += 1\n",
    "            length = int(s[i:j])\n",
    "            i = j + 1\n",
    "            j = i + length\n",
    "            res.append(s[i:j])\n",
    "            i = j\n",
    "        return res\n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        list_ = []\n",
    "        for i in strs:\n",
    "            for j in i:\n",
    "                list_.append(j)\n",
    "            list_.append(\"\\n\")\n",
    "        return \"\".join(list_)\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        ans = []\n",
    "        str_ = \"\"\n",
    "        for i in s:\n",
    "            if i == \"\\n\":\n",
    "                ans.append(str_)\n",
    "                str_ = \"\"\n",
    "            else:\n",
    "                str_ += i\n",
    "        return ans\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        res = ''\n",
    "        num = 0\n",
    "        for c in strs:\n",
    "            num = len(c)\n",
    "            res += str(num) + '#' + c\n",
    "        # print(res)\n",
    "        return res\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "\n",
    "        i = 0\n",
    "        n = len(s)\n",
    "        words = []\n",
    "        while i < n:\n",
    "            j = i\n",
    "            while s[j] != '#':\n",
    "                j += 1\n",
    "            num = int(s[i: j])\n",
    "            j += 1\n",
    "            word = s[j: j+num]\n",
    "            words.append(word)\n",
    "            j += num\n",
    "            i = j\n",
    "        return words\n",
    "\n",
    "                \n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        return '%00%'.join(strs)\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        return s.split('%00%')\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        res = \"\"\n",
    "        for s in strs:\n",
    "            res += str(len(s))+\"#\"+s\n",
    "        return res\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        res, i = [], 0\n",
    "        while i < len(s):\n",
    "            j = i\n",
    "            while s[j]!=\"#\":\n",
    "                j+=1\n",
    "            length = int(s[i:j])\n",
    "            res.append(s[j+1:j+1+length])\n",
    "            i = j+1+length\n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        return (chr(257)).join(strs)\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        result = []\n",
    "        splitword = s.split(chr(257))\n",
    "        for w in splitword:\n",
    "            result.append(w)\n",
    "        return result\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        if len(strs) == 0:\n",
    "            return chr(258)\n",
    "        \n",
    "        return chr(257).join(strs)\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        if s == chr(258): return []\n",
    "        return s.split(chr(257))\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        out = ''\n",
    "        for str_ in strs:\n",
    "            out += f'{len(str_):03}{str_}'\n",
    "        return out\n",
    "\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        pin = 0\n",
    "        s_len = len(s)\n",
    "        out = []\n",
    "        while pin < s_len:\n",
    "            word_len = int(s[pin:pin+3])\n",
    "            pin += 3\n",
    "            out.append(s[pin:pin+word_len])\n",
    "            pin += word_len\n",
    "        return out\n",
    "\n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    # Use 4 bytes as header to indicate the length of string.\n",
    "    # Time O(n) Space O(n)\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        res = []\n",
    "        for s in strs:\n",
    "            length = str(len(s))\n",
    "            header = \"0\" * (4 - len(length)) + length\n",
    "            res.append(header)\n",
    "            res.append(s)\n",
    "        return \"\".join(res)\n",
    "\n",
    "    # Read first 4 bytes to get length. Then read length bytes for content.\n",
    "    # Time O(n) Space O(1)\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        n = len(s)\n",
    "        read = 0\n",
    "        res = []\n",
    "        while read < n:\n",
    "            header = s[read:read+4]\n",
    "            read += 4\n",
    "            res.append(s[read:read+int(header)])\n",
    "            read += int(header)\n",
    "        return res\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        res = []\n",
    "        for char in strs:\n",
    "            if not char:\n",
    "                res.append('\\n')\n",
    "            else:\n",
    "                res.append(char)\n",
    "        return '\\t'.join(strs)\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        strs = s.split('\\t')\n",
    "        res = []\n",
    "        for char in strs:\n",
    "            if char == '\\n':\n",
    "                res.append('')\n",
    "            else:\n",
    "                res.append(char)\n",
    "\n",
    "        return res\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        return '||'.join([s.replace('|', '%|%') for s in strs])\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        return [s.replace('%|%', '|') for s in s.split('||')]\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        s = '(break)'.join(strs)\n",
    "        return s\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        rs = s.split('(break)')\n",
    "        return rs\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        self.index = [0]\n",
    "        for s in strs:\n",
    "            self.index.append(len(s)+self.index[-1])\n",
    "        self.index.pop(0)\n",
    "        return ''.join(strs)\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        left = 0\n",
    "        strs = []\n",
    "        for i in self.index:\n",
    "            strs.append(s[left:i])\n",
    "            left = i\n",
    "        return strs\n",
    "\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        res = ''\n",
    "        for s in strs:\n",
    "            res += str(len(s)) + '#' + s \n",
    "        return res \n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        i = 0 \n",
    "        res = []\n",
    "        while i < len(s):\n",
    "            j = i \n",
    "            while s[j] != '#':\n",
    "                j += 1\n",
    "            length = int(s[i:j])\n",
    "            res.append(s[j + 1: j + 1 + length])\n",
    "            i = j + 1 + length \n",
    "        return res \n",
    "\n",
    "        \n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        if len(strs) == 0: return ''\n",
    "        return ','.join([str(len(s)) for s in strs]) + '#' + ''.join(strs)\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        if not s: return []\n",
    "        i = 0\n",
    "        while s[i] != '#': i += 1\n",
    "        lens = s[:i].split(',')\n",
    "        strs = s[i+1:]\n",
    "        res = []\n",
    "        i = 0\n",
    "        for l in lens:\n",
    "            l = int(l)\n",
    "            end = i + l\n",
    "            res.append(strs[i:end])\n",
    "            i = end\n",
    "        return res\n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        ret=''\n",
    "        for item in strs:\n",
    "            index1=item.find('/') if item.find('/')!=-1 else 0x7fffffff\n",
    "            index2=item.find('*') if item.find('*')!=-1 else 0x7fffffff\n",
    "            index=min(index1,index2)\n",
    "            temp=''\n",
    "            while index!=0x7fffffff:\n",
    "                item=item[:index]+'/'+item[index:]\n",
    "                temp+=item[:index+2]\n",
    "                item=item[index+2:]\n",
    "                index1=item.find('/') if item.find('/')!=-1 else 0x7fffffff\n",
    "                index2=item.find('*') if item.find('*')!=-1 else 0x7fffffff\n",
    "                index=min(index1,index2)\n",
    "            ret=ret+temp+item+'*'\n",
    "        return ret\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        i=0\n",
    "        ret=[]\n",
    "        while i<len(s):\n",
    "            if i==len(s)-1:\n",
    "                ret.append(s[:len(s)-1])\n",
    "                break\n",
    "            if s[i]=='/':\n",
    "                s=s[:i]+s[i+1:]\n",
    "                i+=1\n",
    "            elif s[i]=='*':\n",
    "                ret.append(s[:i])\n",
    "                s=s[i+1:]\n",
    "                i=0\n",
    "            else:\n",
    "                i+=1\n",
    "        return ret\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        ret = []\n",
    "        for s in strs:\n",
    "            l = len(s)\n",
    "            ret.append(f\"{l}#{s}\")\n",
    "        return \"\".join(ret)\n",
    "        \n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        ret = []\n",
    "        i = 0\n",
    "        #print(s)\n",
    "        while i < len(s):\n",
    "            cnt = 0\n",
    "            while i < len(s) and s[i].isdigit():\n",
    "                cnt = cnt * 10 + int(s[i])\n",
    "                i += 1\n",
    "            j = i + 1 + cnt\n",
    "            ret.append(s[i + 1: j])\n",
    "            i = j\n",
    "        return ret\n",
    "            \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "\n",
    "        \"\"\"\n",
    "        \n",
    "        ret = ''\n",
    "        for word in strs:\n",
    "            ret += (str(len(word)) + '#' + word)\n",
    "        return ret\n",
    "\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        \n",
    "        ret = []\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            l = i\n",
    "            while s[i]!= \"#\":\n",
    "                i += 1\n",
    "            length = int(s[l:i])\n",
    "            ret.append(s[i + 1: i + 1 + length])\n",
    "            i = i + 1 + length\n",
    "        return ret\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def int_to_char(self, s):\n",
    "        ans = format(s, 'x')\n",
    "        while len(ans) < 4:\n",
    "            ans = '0' + ans\n",
    "        return ans\n",
    "    \n",
    "    def char_to_int(self, ch):\n",
    "        i = 0\n",
    "        while i < 4 and ch[i] == '0':\n",
    "            i += 1\n",
    "        if i == 4:\n",
    "            return 0\n",
    "        return int(ch[i:], base=16)\n",
    "\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        ans = ''\n",
    "        for s in strs:\n",
    "            l = len(s)\n",
    "            ans += self.int_to_char(l)\n",
    "            ans += s\n",
    "        return ans\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        ans = []\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            pre = s[i:i+4]\n",
    "            l = self.char_to_int(pre)\n",
    "            sstr = s[i+4: i+4+l]\n",
    "            ans.append(sstr)\n",
    "            i = i+4+l\n",
    "        return ans\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        ans1 = str(len(strs)) + '+*/@'\n",
    "        ans2 = ''\n",
    "        for s in strs:\n",
    "            ans1 += str(len(ans2)) + '+*/@'\n",
    "            ans2 += s\n",
    "\n",
    "        ans1 += str(len(ans2)) + '+*/@'\n",
    "\n",
    "        # print(ans1 + ans2)\n",
    "        return ans1 + ans2\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        s_list = s.split('+*/@')\n",
    "        len_s_list = len(s_list)\n",
    "\n",
    "        n = int(s_list[0])\n",
    "        id_list = []\n",
    "        for i in range(n+1):\n",
    "            id_list.append(int(s_list[i + 1]))\n",
    "        \n",
    "        if n + 3 == len_s_list:\n",
    "            ss = s_list[-1]\n",
    "        else:\n",
    "            ss = '+*/'.join(s[n + 2:])\n",
    "\n",
    "        ans = []\n",
    "        for i in range(len(id_list)-1):\n",
    "            ans.append(ss[id_list[i]: id_list[i + 1]])\n",
    "        \n",
    "        # print(i)\n",
    "        # if i != 0:\n",
    "        #     ans.append(ss[id_list[i+1]:]) \n",
    "        \n",
    "        return ans\n",
    "        \n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def __init__(self):\n",
    "        self.index_map = {}\n",
    "\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        if len(strs) == 1:\n",
    "            return strs[0]\n",
    "        encoded_str = \"\"\n",
    "        idx = 0\n",
    "        for i in range(len(strs)):\n",
    "            self.index_map[idx+len(strs[i])-1] = self.index_map.get(idx+len(strs[i])-1, 0) + 1\n",
    "            idx += len(strs[i])\n",
    "            encoded_str += strs[i]\n",
    "        print(encoded_str)\n",
    "        print(self.index_map)\n",
    "        return encoded_str\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        if not self.index_map:\n",
    "            return [s]\n",
    "        start = 0\n",
    "        res = []\n",
    "        for end in range(-1, len(s)):\n",
    "            if end in self.index_map:\n",
    "                res.append(s[start:end+1])\n",
    "                self.index_map[end] -= 1\n",
    "                while self.index_map[end]:\n",
    "                    res.append(\"\")\n",
    "                    self.index_map[end] -= 1\n",
    "                start = end + 1\n",
    "        return res\n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        out = ''\n",
    "        for str_ in strs:\n",
    "            head = f'{len(str_)}/'\n",
    "            end = '/'\n",
    "            out += f'{head}{str_}{end}'\n",
    "        return out\n",
    "\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        split_s = s.split('/')[:-1]\n",
    "        out = []\n",
    "        cur_s = ''\n",
    "        next_len = -1\n",
    "        while split_s:\n",
    "            cur_s += split_s.pop(0)\n",
    "            if next_len < 0:\n",
    "                next_len = int(cur_s)\n",
    "                cur_s = ''\n",
    "                continue\n",
    "            if len(cur_s) != next_len:\n",
    "                \"\"\"\n",
    "                string might contain '/'\n",
    "                \"\"\"\n",
    "                cur_s += '/'\n",
    "                continue\n",
    "            else:\n",
    "                out.append(cur_s)\n",
    "                cur_s = ''\n",
    "                next_len = -1\n",
    "        return out\n",
    "\n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        out = ''\n",
    "        for str_ in strs:\n",
    "            head = f'{len(str_):03}'\n",
    "            out += f'{head}{str_}'\n",
    "        return out\n",
    "\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        pin = 0\n",
    "        s_len = len(s)\n",
    "        out = []\n",
    "        while pin < s_len:\n",
    "            word_len = int(s[pin:pin+3])\n",
    "            pin += 3\n",
    "            out.append(s[pin:pin+word_len])\n",
    "            pin += word_len\n",
    "        return out\n",
    "\n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        n = len(strs)\n",
    "        \"\"\"\n",
    "        Head shoule be n,\n",
    "        \"\"\"\n",
    "        str_lens = [str(len(s)) for s in strs]\n",
    "\n",
    "        return f\"{n},\"+\",\".join(str_lens)+\",\"+\"\".join(strs)\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        n = int(s.split(',')[0])\n",
    "        str_lens = s.split(',')[1: n+1]\n",
    "        head_len = len(str(n)) + sum([len(l) for l in str_lens]) + len(str_lens) + 1\n",
    "        strs = []\n",
    "        accum = 0\n",
    "        content = s[head_len:]\n",
    "        for l in str_lens:\n",
    "            l = int(l)\n",
    "            sub_s = content[accum: accum + l]\n",
    "            accum += l\n",
    "            strs.append(sub_s)\n",
    "        return strs\n",
    "            \n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        list_ = []\n",
    "        for i in strs:\n",
    "            for j in i:\n",
    "                list_.append(j)\n",
    "            list_.append(\"\\n\")\n",
    "        return \"\".join(list_)\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        ans = []\n",
    "        str_ = \"\"\n",
    "        print(s)\n",
    "        for i in s:\n",
    "            if i == \"\\n\":\n",
    "                ans.append(str_)\n",
    "                str_ = \"\"\n",
    "            else:\n",
    "                str_ += i\n",
    "        return ans\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        res = ''\n",
    "        num = 0\n",
    "        for c in strs:\n",
    "            num = len(c)\n",
    "            res += str(num) + '#' + c\n",
    "        # print(res)\n",
    "        return res\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        idx = 0\n",
    "        n = len(s)\n",
    "        words = []\n",
    "        while idx < n:\n",
    "            if s[idx].isdigit():\n",
    "                start = idx\n",
    "                idx += 1\n",
    "                while s[idx].isdigit():\n",
    "                    idx += 1\n",
    "                num = int(s[start:idx])\n",
    "                idx += 1\n",
    "                word = s[idx:idx+num]\n",
    "                words.append(word)\n",
    "                idx += num\n",
    "\n",
    "        # print(words)\n",
    "        return words\n",
    "\n",
    "                \n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: list[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        List = []\n",
    "        for i in strs:\n",
    "            for j in i:\n",
    "                if j == ' ':\n",
    "                    List.append('\\t')\n",
    "                else:\n",
    "                    List.append(j)\n",
    "            List.append('\\n')\n",
    "        List = ''.join(List)\n",
    "        return List\n",
    "\n",
    "    def decode(self, s: str) -> list[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        List = []\n",
    "        Str = ''\n",
    "        for i in s:\n",
    "            if i == '\\t':\n",
    "                Str = ''.join([Str, ' '])\n",
    "            else:\n",
    "                if i != '\\n':\n",
    "                    Str = ''.join([Str, i])\n",
    "                else:\n",
    "                    List.append(Str)\n",
    "                    Str = ''\n",
    "        return List\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        res = ''\n",
    "        num = 0\n",
    "        for c in strs:\n",
    "            num = len(c)\n",
    "            res += str(num) + '#' + c\n",
    "        # print(res)\n",
    "        return res\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "\n",
    "        idx = 0\n",
    "        n = len(s)\n",
    "        words = []\n",
    "        while idx < n:\n",
    "            if s[idx].isdigit():\n",
    "                start = idx\n",
    "                idx += 1\n",
    "                while s[idx].isdigit():\n",
    "                    idx += 1\n",
    "                num = int(s[start: idx])\n",
    "                idx += 1\n",
    "                word = s[idx: idx+num]\n",
    "                words.append(word)\n",
    "                idx += num\n",
    "        return words\n",
    "\n",
    "                \n",
    "\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        res = \"\"\n",
    "        for strin in strs:\n",
    "            res += f\"{len(strin)}#{strin}\"\n",
    "        return res\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        res = []\n",
    "        i = 0\n",
    "        \n",
    "        while i < len(s):\n",
    "            j = i\n",
    "            while s[j] != '#':\n",
    "                j += 1\n",
    "            length = int(s[i:j])\n",
    "            i = j + 1\n",
    "            j = i + length\n",
    "            res.append(s[i:j])\n",
    "            i = j\n",
    "            \n",
    "        return res\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        send = \"\"\n",
    "        for s in strs:\n",
    "            send = send + str(len(s)) + \"@\" + s\n",
    "        return send\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        received = []\n",
    "        while s:\n",
    "            length = int(s.split(\"@\")[0])\n",
    "            start_idx = s.index(\"@\")\n",
    "            received.append(s[start_idx+1:start_idx+1+length])\n",
    "            s = s[start_idx+1+length:]\n",
    "        return received\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        # 例子：[\"leet\",\"code\"]\n",
    "        # \"4#leet4#code\"\n",
    "        # int: 0 #:1 str_len = int(s[0:1])=4 str1 = s[2:2+4]\n",
    "        res = \"\"\n",
    "        for s in strs:\n",
    "            res += str(len(s)) + \"#\" + s\n",
    "        return res\n",
    "        \n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        ans = []\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            j = i\n",
    "            # 找到第一个分隔符的索引位置\n",
    "            while s[j] != \"#\":\n",
    "                j += 1\n",
    "            \n",
    "            length = int(s[i:j])\n",
    "            ans.append(s[j+1:j+1+length])\n",
    "\n",
    "            i = j+length+1\n",
    "\n",
    "        return ans\n",
    "        \n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Codec:\n",
    "    def encode(self, strs: List[str]) -> str:\n",
    "        \"\"\"Encodes a list of strings to a single string.\n",
    "        \"\"\"\n",
    "        res = \"\"\n",
    "        for s in strs:\n",
    "            res += str(len(s))+\"#\"+s\n",
    "        return res\n",
    "\n",
    "    def decode(self, s: str) -> List[str]:\n",
    "        \"\"\"Decodes a single string to a list of strings.\n",
    "        \"\"\"\n",
    "        res = []\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            j = i\n",
    "            while s[j] != \"#\":\n",
    "                j += 1\n",
    "            length = int(s[i:j])\n",
    "            cur_str = s[j+1:j+1+length]\n",
    "            res.append(cur_str)\n",
    "            i = j+length+1\n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "# Your Codec object will be instantiated and called as such:\n",
    "# codec = Codec()\n",
    "# codec.decode(codec.encode(strs))"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
